import React from "react";
import {Select} from "antd";
import BaseComponent from "../../../redare/components/BaseComponent";
import DictionaryPresenter from "../../../.gen/presenter/DictionaryPresenter";

export default class DictSelect extends BaseComponent {

    static defaultProps = {
        popupcontainer: undefined
    };

    constructor(props) {
        super(props);
        this.dictPresenter = new DictionaryPresenter(this);
        this.state = {
            loading: true,
            dArray: [],
            value: this.props.value,
        }
    }

    componentDidMount() {
        let {code} = this.props;
        if (code) {
            this.loadData({parentCode: code} );
        }else if( this.props.data && this.props.data.constructor === Array){
            this.setState({dArray: this.props.data,loading: false});
        }else {
            setTimeout(() => {
                this.setState({loading: false});
            }, 500);
        }

    }

    resetData(){
        this.value = null;
        this.setState({value: null});
    }

    loadData( body={} ){
        // 只查询启用的
        body.status = 1;
        this.setState({loading: true, value: undefined,dArray: []});
        this.dictPresenter.findDictionaryList(
          body,
          {},
          (success_re) => {
              setTimeout(() => {
                  this.setState({loading: false, dArray: success_re.data});
              }, 500);
          },
          (fail_re) => {
              this.setState({loading: false});
          }
        );
    }

    getData(){

        this.resultData = null;
        let matchkey = 'id';
        if (this.props.matchkey) {
            matchkey = this.props.matchkey;
        }

        let {dArray=[]} = this.state;
        let value = this.value;
        if( value && value.constructor === Array){
            let _newData = [];
            value.map((d_item)=>{
                let _data = dArray.filter((item)=> {
                    return item[matchkey] === d_item;
                });
                _newData.push(_data[0]);
            });
            return _newData;
        }else {
            let _value = value || this.props.value;
            let _data = dArray.filter((item)=> {
                return item[matchkey] === _value;
            });
            return _data[0];

        }

    }


    _onChange = (value, option)=>{
        this.setState({value: value});
        let changeFn = this.props.onChange;
        this.value = value;

        if( changeFn && changeFn.constructor === Function ){
            changeFn(value, option);
        }
    }


    render() {
        let dArray = this.state.dArray;

        let matchkey = 'id';
        if (this.props.matchkey) {
            matchkey = this.props.matchkey;
        }

        let _labelInValue = false;
        let value = this.state.value;
        if( !value && this.props.value && !this.state.loading ){
            value = this.props.value;
            if( value && Object.keys(value).length > 0 && value.value){
                value = value.value;
            }
        }


        let placeholder = this.props.placeholder || '请选择';
        let placeholder_active = '';
        if( this.state.loading ){
            placeholder_active = '加载中...';
        }else {
            placeholder_active = placeholder;
        }

        let popupContainer = this.props.popupcontainer;

        return (
            <Select
                showSearch={true}
                optionFilterProp="children"
                placeholder={placeholder_active}
                style={{width: '100%'}}
                loading={this.state.loading}
                {...this.props}
                onChange={this._onChange}
                value={value}
                getPopupContainer={(triggerNode)=>{
                    if( popupContainer ){
                        return popupContainer;
                    }else {
                        return triggerNode.parentElement;
                    }
                }}
            >
                {
                    dArray.map((item) => {
                        return <Select.Option key={"option_" + item.id} value={item[matchkey]}
                                              data={item}>{item.dicName}</Select.Option>
                    })
                }
            </Select>
        );
    }

    //加载数据
    setRequestData(requestPromiseFn) {
        if (!requestPromiseFn) {
            return;
        }
        this.setState({loading: true, dArray: []});
        requestPromiseFn().then((data) => {
            let array = [];
            if (!data || data.constructor === Array) {
                array = data;
            }
            this.setState({dArray: array, loading: false});
        }).catch(() => {
            this.setState({loading: false});
        });
    }

    setSourceLoading(){
        this.setState({loading: true});
        setTimeout(()=>{
            this.setState({loading: false});
        }, 3000);
    }
    // 设置源数据
    setSourceData(data){
        this.setState({dArray: [...data], loading: false });
    }


}


